﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestOnlineProject.Models.Entities;
using TestOnlineProject.Models.BusinessLogic;
using PagedList;

namespace TestOnlineProject.Controllers
{

    public class ReportController : Controller
    {
        ReportBLO reportBLO = new ReportBLO();
        DiscussionBLO discussionBLO = new DiscussionBLO();
        QuestionBLO questionBLO = new QuestionBLO();
        TestBLO testBLO = new TestBLO();
        SectionBLO sectionBLO = new SectionBLO();

        [Authorize(Roles = "1")]
        public ActionResult Index(int? page, string keyWord = "", string column = "", string sort = "")
        {
            try
            {
                // No. list.
                var noList = new List<NoModel>();

                // Violation list.
                List<Report> reports = reportBLO.ToList(ref noList, page, keyWord, column, sort);

                // Sort states.
                ViewBag.KeyWord = keyWord;
                ViewBag.Page = page;
                ViewBag.Column = column;
                ViewBag.Sort = sort;
                ViewBag.NoList = noList;

                // Return.
                var pageNumber = page ?? 1;
                var onePageOfReports = reports.ToPagedList(pageNumber, 10);
                ViewBag.onePageOfReports = onePageOfReports;
                return Request.IsAjaxRequest()
                    ? (ActionResult)PartialView("_List")
                    : View();
            }
            catch (Exception)
            {
                // Wrtite to log file.
                return RedirectToAction("Index", "Error", new { Area = "" });
            }
        }

        public string Create(int sectionId = 0, int questionId = 0, int testId = 0, int discussionId = 0, string message = "")
        {
            try
            {
                string result = reportBLO.Add(sectionId, questionId, testId, discussionId, message);

                if (result.Equals("AddSuccessfully"))
                {
                    return "Send successfully.";
                }

                if (result.Equals("Error24H"))
                {
                    return "Just wait for admin to check your last report.";
                }

                if (result.Equals("Error"))
                {
                    return "An error occurred.";
                }

                return "";
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }

        [Authorize(Roles = "1")]
        public ActionResult Details(int id = 0, string isChecked = "")
        {
            var report = reportBLO.Find(id);
            if (report == null)
            {
                return HttpNotFound();
            }
            if (isChecked.Equals("checked"))
            {
                reportBLO.Check(id + "-");
            }
            ViewBag.Reports = reportBLO.ToList().OrderByDescending(r => r.ReportId).ToList();
            return View(report);
        }

        [Authorize(Roles = "1")]
        public string Uncheck(string ids = "")
        {
            try
            {
                int result = reportBLO.Uncheck(ids);
                if (result > 0)
                {
                    if (result == 1)
                    {
                        return "The report has been marked as unread.";
                    }
                    else
                    {
                        return result + " reports has been marked as unread.";
                    }
                }
                else
                {
                    return "An error occurred.";
                }
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }

        [Authorize(Roles = "1")]
        public string Check(string ids = "")
        {
            try
            {
                int result = reportBLO.Check(ids);
                if (result > 0)
                {
                    if (result == 1)
                    {
                        return "The report has been marked as read.";
                    }
                    else
                    {
                        return result + " reports has been marked as read.";
                    }
                }
                else
                {
                    return "An error occurred.";
                }
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }

        [Authorize(Roles = "1")]
        public string Delete(string ids = "")
        {
            try
            {
                int result = reportBLO.Remove(ids);
                if (result > 0)
                {
                    if (result == 1)
                    {
                        return "The report has been deleted.";
                    }
                    else
                    {
                        return result + " reports has been deleted.";
                    }
                }
                else
                {
                    return "An error occurred.";
                }
            }
            catch (Exception)
            {
                // Write to log file.
                return "An error occurred.";
            }
        }

    }
}
